package zxq.rollcall.course.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import zxq.rollcall.course.controller.action.ICollegeController;
import zxq.rollcall.course.entity.SystemCollegeTableEntity;
import zxq.rollcall.course.entity.dto.ResultDTO;
import zxq.rollcall.course.entity.dto.set.ClassDTO;
import zxq.rollcall.course.entity.dto.set.CollegeDTO;
import zxq.rollcall.course.entity.query.CollegeQuery;
import zxq.rollcall.course.entity.query.PageQuery;
import zxq.rollcall.course.service.MPSystemClassTableService;
import zxq.rollcall.course.service.MPSystemCollegeTableService;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author Yanghu
 * @since 2020-01-22
 */
@Controller
@RequestMapping("/course/api/college")
@Api(tags = "学校部门表")
public class SystemCollegeTableAction implements ICollegeController {

    @Autowired
    MPSystemCollegeTableService mpSystemCollegeTableService;

    @Override
    public ResultDTO<Boolean> save(CollegeQuery collegeQuery) {
        SystemCollegeTableEntity systemCollegeTableEntity = new SystemCollegeTableEntity();
        BeanUtils.copyProperties(collegeQuery,systemCollegeTableEntity);
        boolean rst = mpSystemCollegeTableService.save(systemCollegeTableEntity);
        if(rst) {
            return new ResultDTO<Boolean>(1,"插入成功",true);
        }
        return new ResultDTO<Boolean>(0,"插入失败",false);
    }

    @Override
    public ResultDTO<Boolean> update(Integer id, CollegeQuery collegeQuery) {
        SystemCollegeTableEntity systemCollegeTableEntity = new SystemCollegeTableEntity();
        BeanUtils.copyProperties(collegeQuery,systemCollegeTableEntity);
        systemCollegeTableEntity.setId(id);
        boolean rst = mpSystemCollegeTableService.updateById(systemCollegeTableEntity);
        if(rst) {
         return new ResultDTO<>(1,"更新成功",true);
        }
        return new ResultDTO<>(0,"更新失败",false);
    }

    @Override
    public ResultDTO<Boolean> delete(Integer id) {
        boolean rst = mpSystemCollegeTableService.removeById(id);
        if(rst) {
            return new ResultDTO<>(1,"删除成功",true);
        }
        return new ResultDTO<>(0,"删除失败",false);
    }

    @Override
    public ResultDTO<List<CollegeDTO>> query(PageQuery pageQuery) {
        Page<SystemCollegeTableEntity> page = new Page(pageQuery.getPage(),pageQuery.getCount());
        page = mpSystemCollegeTableService.page(page);
        List<CollegeDTO> list = page.getRecords().stream().map(item -> {
            CollegeDTO collegeDTO = new CollegeDTO();
            BeanUtils.copyProperties(item,collegeDTO);
            return collegeDTO;
        }).collect(Collectors.toList());
        ResultDTO resultDTO = new ResultDTO(1,"查询成功",list);
        return resultDTO;
    }

}

